Sorts an array using default and custom comparer - keys
The following example shows how to sort the values in an array using the default comparer and a custom comparer that reverses the sort order.
C# .NET |
public class myReverserClass : IComparer { // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare( Object x, Object y ) { return( (new CaseInsensitiveComparer()).Compare( y, x ) ); } } public static String DoTest() { string strRet = ""; // Creates and initializes a new Array and a new custom comparer. String[] myKeys = { "red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange" }; String[] myValues = { "strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe" }; IComparer myComparer = new myReverserClass(); // Displays the values of the Array. strRet += ( "The Array initially contains the following values:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts a section of the Array using the default comparer. Array.Sort( myKeys, myValues, 1, 3 ); strRet += ( "After sorting a section of the Array using the default comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts a section of the Array using the reverse case-insensitive comparer. Array.Sort( myKeys, myValues, 1, 3, myComparer ); strRet += ( "After sorting a section of the Array using the reverse case-insensitive comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts the entire Array using the default comparer. Array.Sort( myKeys, myValues ); strRet += ( "After sorting the entire Array using the default comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts the entire Array using the reverse case-insensitive comparer. Array.Sort( myKeys, myValues, myComparer ); strRet += ( "After sorting the entire Array using the reverse case-insensitive comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); return strRet; } public static string PrintKeysAndValues( String[] myKeys, String[] myValues ) { string strRet = "\r\n"; for ( int i = 0; i < myKeys.Length; i++ ) { strRet += String.Format(" {0,-10}: {1}", myKeys[i], myValues[i] ); strRet += "\r\n"; } strRet += "\r\n"; return strRet; } |
Blaze++ .NET |
class myReverserClass : public IComparer { public: // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer::Compare( Object& x, Object& y ) { CaseInsensitiveComparer cic; return cic.Compare( y, x ); } }; static String DoTest() { String strRet = ""; // Creates and initializes a new Array and a new custom comparer. String keys[] = { "red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange" }; Array myKeys(keys, 7); String values[] = { "strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe" }; Array myValues(values, 7); myReverserClass myComparer; // Displays the values of the Array. strRet += ( "The Array initially contains the following values:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts a section of the Array using the default comparer. Array::Sort( myKeys, myValues, 1, 3 ); strRet += ( "After sorting a section of the Array using the default comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts a section of the Array using the reverse case-insensitive comparer. Array::Sort( myKeys, myValues, 1, 3, myComparer ); strRet += ( "After sorting a section of the Array using the reverse case-insensitive comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts the entire Array using the default comparer. Array::Sort( myKeys, myValues ); strRet += ( "After sorting the entire Array using the default comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); // Sorts the entire Array using the reverse case-insensitive comparer. Array::Sort( myKeys, myValues, myComparer ); strRet += ( "After sorting the entire Array using the reverse case-insensitive comparer:" ); strRet += PrintKeysAndValues( myKeys, myValues ); return strRet; } static String PrintKeysAndValues(Array& myKeys, Array& myValues ) { String strRet = "\r\n"; for ( int i = 0; i < myKeys.Length; i++ ) { strRet += String::Format(" {0,-10}: {1}", myKeys[i], myValues[i] ); strRet += "\r\n"; } strRet += "\r\n"; return strRet; } |